package com.szx.usercenter.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szx.usercenter.domain.SysRole;
import com.szx.usercenter.domain.SysUser;
import com.szx.usercenter.domain.SysUserRole;
import com.szx.usercenter.mapper.SysUserRoleMapper;
import com.szx.usercenter.service.SysRoleService;
import com.szx.usercenter.service.SysUserRoleService;
import com.szx.usercenter.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 25019
 * @description 针对表【sys_user_role(用户和角色关联表)】的数据库操作Service实现
 * @createDate 2024-05-19 08:31:02
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole>
        implements SysUserRoleService {
    @Autowired
    SysRoleService sysRoleService;

    // 添加用户角色
    @Override
    public Response addUserRole(List<Long> roleIds, Long userId) {
        if (roleIds.size() == 0) {
            return Response.error("角色信息不能为空");
        }

        // 删除这个用户当前的所有角色
        LambdaUpdateWrapper<SysUserRole> uw = new LambdaUpdateWrapper<>();
        uw.eq(SysUserRole::getUserId, userId);
        this.remove(uw);

        // 追加新的角色
        List<SysUserRole> sysUserRoleList = new ArrayList<>();
        roleIds.forEach(roleId -> {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(userId);
            sysUserRole.setRoleId(roleId);
            sysUserRoleList.add(sysUserRole);
        });
        boolean resource = this.saveBatch(sysUserRoleList);
        return resource ? Response.success() : Response.error();
    }

    // 根用户添加默认角色
    @Override
    public void setUserDefaultRole(SysUser sysUser) {
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(sysUser.getUserId());
        sysUserRole.setRoleId(101L);
        this.save(sysUserRole);
    }

    // 根据用户ID获取角色列表
    @Override
    public List<SysRole> getUserRoleList(Long userId) {
        List<SysUserRole> roleList = this.list(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userId));
        if (ObjectUtil.isNotEmpty(roleList)) {
            List<Long> roleIds = roleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());

            LambdaQueryWrapper<SysRole> roleQw = new LambdaQueryWrapper<>();
            roleQw.in(SysRole::getRoleId, roleIds);
            return sysRoleService.list(roleQw);
        } else {
            return new ArrayList<>();
        }
    }

    // 删除用户角色
    @Override
    public void removeUserRoles(Long userId) {
        LambdaUpdateWrapper<SysUserRole> qw = new LambdaUpdateWrapper<>();
        qw.eq(SysUserRole::getUserId, userId);
        this.remove(qw);
    }
}




